package com.example.ruiji.controller.editormd;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.ruiji.dao.mapper.TagMapper;
import com.example.ruiji.dao.mapper.UserMapper;
import com.example.ruiji.pojo.Article;
import com.example.ruiji.pojo.Tag;
import com.example.ruiji.pojo.User;
import com.example.ruiji.service.article.ArticleServiceImpl;
import com.example.ruiji.utils.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date;
import java.util.UUID;

/**
 * Author:BaiYiChen
 * Date:2022/4/26 16:21
 *
 * @author BaiYiChen
 */
@RestController
public class EditormdController {
  @Autowired
  private ArticleServiceImpl articleService;
  @Autowired
  private TagMapper tagMapper;
  @Autowired
  private UserMapper userMapper;
  
  @RequestMapping("/w/editormdPic")
  @ResponseBody
  public Result editorPic(@RequestParam(value = "editormd-image-file", required = true) MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception{
    
    String trueFileName = file.getOriginalFilename();
    
    String suffix = trueFileName.substring(trueFileName.lastIndexOf("."));
    
    String fileName = System.currentTimeMillis()+"_"+ UUID.randomUUID().toString()+suffix;
    
    String path = request.getSession().getServletContext().getRealPath("/assets/msg/upload/");
    System.out.println(path);
    
    File targetFile = new File(path, fileName);
    if(!targetFile.exists()){
      targetFile.mkdirs();
    }
    
    //保存
    try {
      file.transferTo(targetFile);
    } catch (Exception e) {
      e.printStackTrace();
    }
    
    JSONObject res = new JSONObject();
    res.put("url","assets/msg/upload/"+fileName);
    res.put("success", 1);
    res.put("message", "upload success!");
    
    return Result.successful(res);
    
  }
  @RequestMapping("/editormdPic")
    public Result getEditormdPic(HttpServletRequest request,HttpServletResponse res){
/**设置响应头允许ajax跨域访问**/
    res.setHeader("Access-Control-Allow-Origin","*");
    /*星号表示所有的异域请求都可以接受，*/
    res.setHeader("Access-Control-Allow-Methods","GET,POST");
    
    String formData = request.getParameter("formData");
    JSONObject jsonObject = JSONObject.parseObject(formData);
    String data = request.getParameter("data");
    System.out.println("表单数据"+formData);
    System.out.println("文章数据"+data);
    
    String title = jsonObject.getString("title");
    String tags1 = jsonObject.getString("tags");
    String[] tagSplit = tags1.split(",");
    
    Article article = new Article(0, 0, new Date(), data,title, 0, 0, 101, 2, 2);
    Integer authorId = article.getAuthorId();
    Tag tags=null;
    User user=null;
    if (articleService.insertArticle(article)>0) {
      Integer articleId = article.getId();
//      user =new User(articleId,"浅斟低唱",true,null,new Date(),false,"2332869185@qq.com",new Date(1619780669000L),"19882026235","包子","123456","abchsjk","在线");
//      userMapper.insert(user);
      for (String tag : tagSplit) {
          tags=new Tag(0,articleId,Integer.parseInt(tag));
          tagMapper.insert(tags);
      }
      
      return Result.successful("插入成功！");
    }
    return Result.successful("插入失败，filed");
  }
}

